home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / WASTE 1.2 / WASTE Demo ƒ / DialogUtils.c next >
Text File  |  1996-05-19  |  4KB  |  151 lines

  1. /*
  2.     WASTE Demo Project:
  3.     Dialog Utilities
  4.  
  5.     Copyright © 1993-1996 Marco Piovanelli
  6.     All Rights Reserved
  7.  
  8.     C port by John C. Daub
  9. */
  10.  
  11. #ifndef __DIALOGS__
  12. #include <Dialogs.h>
  13. #endif
  14.  
  15. #ifndef __WEDEMOAPP__
  16. #include "WEDemoIntf.h"
  17. #endif
  18.  
  19. static pascal Boolean MyStandardDialogFilter( DialogRef dialog, EventRecord *event, short *item )
  20. {
  21.     GrafPtr                savePort;
  22.     ModalFilterUPP        stdFilter = nil;
  23.     Boolean                retval = false;
  24.     OSErr                err;
  25.  
  26.     // set up the port
  27.     GetPort( &savePort );
  28.     SetGrafPortOfDialog( dialog );
  29.  
  30.     //     intercept window events directed to windows behind the dialog
  31.     if ( ( event->what == updateEvt ) || ( event->what == activateEvt ) )
  32.     {
  33.         if ( ((WindowRef) event->message) != GetDialogWindow( dialog ) )
  34.         {
  35.             DoWindowEvent( event );
  36.         }
  37.     }
  38.  
  39.     // is the default item a pushbutton?
  40.     if ( GetDialogItemType( dialog, GetDialogDefaultItem( dialog ) ) == kButtonDialogItem )
  41.     {
  42.         // yes, so tell the Dialog Manager to care about its outline
  43.         SetDialogDefaultItem( dialog, GetDialogDefaultItem( dialog ));
  44.     }
  45.  
  46.     // this is something not in the original WASTE Demo App, but in the work that I've done
  47.     // on my own projects, I've found it useful and helpful.
  48.  
  49.     // let's also make sure the cancel button can be handled...now, the cancel button
  50.     // should be dialog item #2.  So, we get dialog item #2, check if it's a button.
  51.     // if it fills these 2 criteria, it's cancel.  Even if the default item and the
  52.     // cancel item are the same, still let them both be set this way so whatever keyboard
  53.     // keys sthe user presses will be handled properly
  54.  
  55.     // pass the number "2" to GetDialogItemType...don't check for the cancel item (cause tho
  56.     // cancel is defined as 2, we're not looking for cancel, we're looking for dialog item #2
  57.     // this is just more readable code.
  58.  
  59.     // remember, this assumes that your cancel item will be item #2 (or at least the item
  60.     // that you want to use for cancelling is #2), and that there are at least 2 items in
  61.     // the dialog to begin with!
  62.  
  63.     if ( GetDialogItemType( dialog, kStdCancelItemIndex ) == kButtonDialogItem )
  64.     {
  65.         SetDialogCancelItem( dialog, kStdCancelItemIndex );
  66.     }
  67.  
  68.     //    call the standard Dialog Manager filter procedure
  69.  
  70.     if ( ( ( err = GetStdFilterProc( &stdFilter ) ) == noErr ) && ( stdFilter != nil ) )
  71.     {
  72.         retval = CallModalFilterProc( stdFilter, dialog, event, item );
  73.     }
  74.  
  75.     //    restore the port
  76.     SetPort( savePort );
  77.  
  78.     return retval;
  79. }
  80.  
  81. ModalFilterUPP GetMyStandardDialogFilter( void )
  82. {
  83. #ifdef __cplusplus
  84.     static ModalFilterUPP sFilterUPP = NewModalFilterProc( MyStandardDialogFilter );
  85. #else
  86.     static ModalFilterUPP sFilterUPP = nil;
  87.  
  88.     if ( sFilterUPP == nil )
  89.     {
  90.         sFilterUPP = NewModalFilterProc( MyStandardDialogFilter );
  91.     }
  92. #endif
  93.  
  94.     return sFilterUPP;
  95. }
  96.  
  97. short GetDialogItemType( DialogRef dialog, short item )
  98. {
  99.     short        itemType;
  100.     Handle        itemHandle;
  101.     Rect        itemRect;
  102.  
  103.     GetDialogItem( dialog, item, &itemType, &itemHandle, &itemRect );
  104.  
  105.     return itemType;
  106. }
  107.  
  108. Handle GetDialogItemHandle( DialogRef dialog, short item )
  109. {
  110.     short        itemType;
  111.     Handle        itemHandle;
  112.     Rect        itemRect;
  113.  
  114.     GetDialogItem( dialog, item, &itemType, &itemHandle, &itemRect );
  115.  
  116.     return itemHandle;
  117. }
  118.  
  119. void GetDialogItemRect( DialogRef dialog, short item, Rect *itemRect )
  120. {
  121.     short        itemType;
  122.     Handle        itemHandle;
  123.  
  124.     GetDialogItem( dialog, item, &itemType, &itemHandle, itemRect );
  125. }
  126.  
  127. void SetDialogItemProc( DialogRef dialog, short item, UserItemUPP proc )
  128. {
  129.     short        itemType;
  130.     Handle        itemHandle;
  131.     Rect        itemRect;
  132.  
  133.     GetDialogItem( dialog, item, &itemType, &itemHandle, &itemRect );
  134.  
  135.     if ( ( itemType & 0x007F) == userItem )
  136.     {
  137.         SetDialogItem( dialog, item, itemType, (Handle) proc, &itemRect );
  138.     }
  139. }
  140.  
  141. void FlashButton( DialogRef dialog, short item )
  142. {
  143.     ControlRef button;
  144.     long finalTicks;
  145.  
  146.     button = (ControlRef) GetDialogItemHandle( dialog, item );
  147.     HiliteControl( button, kControlButtonPart );
  148.     Delay( 8, &finalTicks );
  149.     HiliteControl( button, kControlNoPart );
  150. }
  151.